package com.gin.no_gay_alliance.fragment.user.tab;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gin.nga.method.NukeApi;
import com.gin.nga.response.body.nuke.MedalInfoBody;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.adpter.forum.ForumEntryAdapter;
import com.gin.no_gay_alliance.adpter.medal.MedalInUserCenterAdapter;
import com.gin.no_gay_alliance.adpter.user.UserBuffAdapter;
import com.gin.no_gay_alliance.adpter.user.UserReputationAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.CommonCallback;
import com.gin.no_gay_alliance.databinding.FragmentUserCenterTabUserInfoExtra2Binding;
import com.gin.no_gay_alliance.databinding.ItemForumEntryBinding;
import com.gin.no_gay_alliance.databinding.ItemTitleRecyclerViewBinding;
import com.gin.no_gay_alliance.itemdecoration.SimpleItemDecoration;
import com.gin.no_gay_alliance.utils.ViewUtils;
import com.gin.no_gay_alliance.viewmodel.EntryData;
import com.gin.no_gay_alliance.viewmodel.KeyValue;
import com.gin.no_gay_alliance.viewmodel.ReputationModel;
import com.gin.no_gay_alliance.viewmodel.UserBuffModel;
import com.gin.no_gay_alliance.viewmodel.UserInfoModel;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * 用户额外信息
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/6 23:55
 **/
public class UserInfoExtraTabFragment extends BaseUserCenterTabFragment<FragmentUserCenterTabUserInfoExtra2Binding> {

    private MedalInUserCenterAdapter medalAdapter;
    private UserBuffAdapter userBuffAdapter;

    @Override
    protected void onCreateView() {
        ArrayList<Object> list = new ArrayList<>();
        list.add(userInfoModel.reputations);
        list.add(userInfoModel.userBuffModels);
        list.add(userInfoModel.adminForums);
        list.add(userInfoModel.medalIds);
        b.rv.setAdapter(new BaseBindingRecyclerViewAdapter<ItemTitleRecyclerViewBinding, Object>(requireContext(), list) {
            @Override
            protected void onBindViewHolder(ViewHolder<ItemTitleRecyclerViewBinding> holder, @NonNull ItemTitleRecyclerViewBinding b, int position, Object item) {
                if (item == userInfoModel.adminForums) {
                    configAdminForums(b);
                }
                if (item == userInfoModel.userBuffModels) {
                    configBuffs(b);
                }
                if (item == userInfoModel.medalIds) {
                    configMedals(b);
                }
                if (item == userInfoModel.reputations) {
                    configReputations(b);
                }
            }

            @NonNull
            @Override
            protected ItemTitleRecyclerViewBinding buildBinding(LayoutInflater inflater) {
                return ItemTitleRecyclerViewBinding.inflate(inflater);
            }

            @Override
            protected boolean useZebraStripe() {
                return false;
            }
        });
    }

    /**
     * 配置管理版面
     *
     * @param binding
     */
    private void configAdminForums(ItemTitleRecyclerViewBinding binding) {
        if (!ObjectUtils.isEmpty(userInfoModel.adminForums)) {
            ViewUtils.setVisibility(View.VISIBLE, binding.rv, binding.tvTitle);
            List<EntryData> entryData = userInfoModel.adminForums.stream().map(KeyValue::toEntryData).collect(Collectors.toList());
            ForumEntryAdapter adapter = new ForumEntryAdapter(requireContext(), entryData) {
                @Override
                protected void onBindViewHolder(ViewHolder<ItemForumEntryBinding> holder, @NonNull ItemForumEntryBinding b, int position, EntryData item) {
                    super.onBindViewHolder(holder, b, position, item);
                    // 如果有个人版在其中，添加前缀
                    if (item.name.equals(userInfoModel.userForum)) {
                        b.getRoot().setText(String.format(Locale.CHINA, "[个]%s", item.name));
                    }
                }
            };
            GridLayoutManager layoutManager = new GridLayoutManager(requireContext(), 3, LinearLayoutManager.VERTICAL, false);
            binding.rv.setLayoutManager(layoutManager);
            binding.rv.setAdapter(adapter);
            binding.tvTitle.setText(String.format(Locale.CHINA, "管理的版面(%d)", entryData.size()));
        } else {
            ViewUtils.setVisibility(View.GONE, binding.rv, binding.tvTitle);
        }

    }

    /**
     * 配置buff
     *
     * @param binding
     */
    private void configBuffs(ItemTitleRecyclerViewBinding binding) {
        List<UserBuffModel> buffs = userInfoModel.userBuffModels;
        if (!ObjectUtils.isEmpty(buffs)) {
            ViewUtils.setVisibility(View.VISIBLE, binding.rv, binding.tvTitle);
            userBuffAdapter = new UserBuffAdapter(requireContext(), buffs);
            binding.rv.setAdapter(userBuffAdapter);
            binding.tvTitle.setText(String.format(Locale.CHINA, "用户状态(%d)", buffs.size()));
        } else {
            ViewUtils.setVisibility(View.GONE, binding.rv, binding.tvTitle);
        }

    }

    /**
     * 配置徽章
     *
     * @param binding
     */
    private void configMedals(ItemTitleRecyclerViewBinding binding) {
        List<String> medalIds = userInfoModel.medalIds;
        if (!ObjectUtils.isEmpty(medalIds)) {
            ViewUtils.setVisibility(View.VISIBLE, binding.rv, binding.tvTitle);
            GridLayoutManager layoutManager = new GridLayoutManager(requireContext(), 2, LinearLayoutManager.VERTICAL, false);
            medalAdapter = new MedalInUserCenterAdapter(requireContext());
            binding.rv.addItemDecoration(new SimpleItemDecoration());
            binding.rv.setLayoutManager(layoutManager);
            binding.rv.setAdapter(medalAdapter);
            binding.tvTitle.setText(String.format(Locale.CHINA, "徽章(%d)", medalIds.size()));

            //  发送请求更新数据
            NukeApi.medalInfoList(MyApplication.getClient(), medalIds.stream().map(Integer::parseInt).collect(Collectors.toList()))
                    .async(new CommonCallback<MedalInfoBody>(handler) {
                        @Override
                        public void onSuccess(MedalInfoBody body) {
                            ui(() -> medalAdapter.updateData(new ArrayList<>(body.getData().values())));
                        }
                    });
        } else {
            ViewUtils.setVisibility(View.GONE, binding.rv, binding.tvTitle);
        }
    }

    /**
     * 配置声望
     *
     * @param binding
     */
    private void configReputations(ItemTitleRecyclerViewBinding binding) {
        List<ReputationModel> reputations = userInfoModel.reputations;
        if (!ObjectUtils.isEmpty(reputations)) {
            ViewUtils.setVisibility(View.VISIBLE, binding.rv, binding.tvTitle);
            binding.rv.setAdapter(new UserReputationAdapter(requireContext(), userInfoModel.reputations));
            binding.tvTitle.setText(String.format(Locale.CHINA, "声望(%d)", reputations.size()));
        } else {
            ViewUtils.setVisibility(View.GONE, binding.rv, binding.tvTitle);
        }

    }

    /**
     * 实例化
     *
     * @param userInfoModel 用户信息
     * @return Fragment
     */
    public static UserInfoExtraTabFragment newInstance(UserInfoModel userInfoModel) {
        UserInfoExtraTabFragment fragment = new UserInfoExtraTabFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_USER_INFO, userInfoModel);
        fragment.setArguments(args);
        return fragment;
    }
}
